In this project we will extract financial data like historical share price and quarterly revenue reportings from various sources using Python libraries and webscraping on popular stocks. After collecting this data we will visualize it in a dashboard to identify patterns or trends. The stocks we will work with are Tesla, Amazon, AMD, and GameStop.
A dashboard often provides a view of key performance indicators in a clear way. Analyzing a data set and extracting key performance indicators will be practiced. Prompts will be used to support learning in accessing and displaying data in dashboards.
A company's stock share is a piece of the company; more precisely: A stock (also known as equity) is a security that represents the ownership of a fraction of a corporation. This entitles the owner of the stock to a proportion of the corporation's assets and profits equal to how much stock they own. Units of stock are called "shares."
#!pip install yfinance
#!pip install pandas
#!pip install requests
#!pip install bs4
!pip install plotly
Requirement already satisfied: plotly in c:\users\shannu\appdata\local\programs\python\python38-32\lib\site-packages (4.14.3) Requirement already satisfied: six in c:\users\shannu\appdata\local\programs\python\python38-32\lib\site-packages (from plotly) (1.15.0) Requirement already satisfied: retrying>=1.3.3 in c:\users\shannu\appdata\local\programs\python\python38-32\lib\site-packages (from plotly) (1.3.3)
WARNING: You are using pip version 21.0.1; however, version 21.1.2 is available. You should consider upgrading via the 'c:\users\shannu\appdata\local\programs\python\python38-32\python.exe -m pip install --upgrade pip' command.
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
In this section, we define the function make_graph. You don't have to know how the function works, you should only care about the inputs. It takes a dataframe with stock data (dataframe must contain Date and Close columns), a dataframe with revenue data (dataframe must contain Date and Revenue columns), and the name of the stock.
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data.Date, infer_datetime_format=True), y=stock_data.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data.Date, infer_datetime_format=True), y=revenue_data.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is Tesla and its ticker symbol is TSLA.
tesla=yf.Ticker("TSLA")
Using the ticker object and the function history extract stock information and save it in a dataframe named tesla_data. Set the period parameter to max so we get information for the maximum amount of time.
tesla_data=tesla.history(period="max")
Reset the index using the reset_index(inplace=True) function on the tesla_data DataFrame and display the first five rows of the tesla_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 1 to the results below.
tesla_data.reset_index(inplace=True)
tesla_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2010-06-29 | 3.800 | 5.000 | 3.508 | 4.778 | 93831500 | 0 | 0.0 |
| 1 | 2010-06-30 | 5.158 | 6.084 | 4.660 | 4.766 | 85935500 | 0 | 0.0 |
| 2 | 2010-07-01 | 5.000 | 5.184 | 4.054 | 4.392 | 41094000 | 0 | 0.0 |
| 3 | 2010-07-02 | 4.600 | 4.620 | 3.742 | 3.840 | 25699000 | 0 | 0.0 |
| 4 | 2010-07-06 | 4.000 | 4.000 | 3.166 | 3.222 | 34334500 | 0 | 0.0 |
Using the requests library to download the webpage https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue. Save the text of the response as a variable named html_data.
html_data=requests.get("https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue").text
Parse the html data using beautiful_soup.
soup=BeautifulSoup(html_data)
Using beautiful soup extract the table with Tesla Quarterly Revenue and store it into a dataframe named tesla_revenue. The dataframe should have columns Date and Revenue. Make sure the comma and dollar sign is removed from the Revenue column.
##Finding the index of required table in the list of tables
tables=soup.find_all('table')
for i,j in enumerate(tables):
if "Tesla Quarterly Revenue" in str(j):
ind=i
fin_tab=tables[ind].find_all('tr')
tesla_revenue=pd.DataFrame(columns=["Date","Revenue"])
for row in fin_tab:
cols=row.find_all("td")
if (cols!=[]):
date=cols[0].text
rev=cols[1].text.replace("$",'').replace(",",'')
tesla_revenue=tesla_revenue.append({"Date":date,"Revenue":rev},ignore_index=True)
tesla_revenue.head()
| Date | Revenue | |
|---|---|---|
| 0 | 2021-03-31 | 10389 |
| 1 | 2020-12-31 | 10744 |
| 2 | 2020-09-30 | 8771 |
| 3 | 2020-06-30 | 6036 |
| 4 | 2020-03-31 | 5985 |
tesla_revenue
| Date | Revenue | |
|---|---|---|
| 0 | 2021-03-31 | 10389 |
| 1 | 2020-12-31 | 10744 |
| 2 | 2020-09-30 | 8771 |
| 3 | 2020-06-30 | 6036 |
| 4 | 2020-03-31 | 5985 |
| 5 | 2019-12-31 | 7384 |
| 6 | 2019-09-30 | 6303 |
| 7 | 2019-06-30 | 6350 |
| 8 | 2019-03-31 | 4541 |
| 9 | 2018-12-31 | 7226 |
| 10 | 2018-09-30 | 6824 |
| 11 | 2018-06-30 | 4002 |
| 12 | 2018-03-31 | 3409 |
| 13 | 2017-12-31 | 3288 |
| 14 | 2017-09-30 | 2985 |
| 15 | 2017-06-30 | 2790 |
| 16 | 2017-03-31 | 2696 |
| 17 | 2016-12-31 | 2285 |
| 18 | 2016-09-30 | 2298 |
| 19 | 2016-06-30 | 1270 |
| 20 | 2016-03-31 | 1147 |
| 21 | 2015-12-31 | 1214 |
| 22 | 2015-09-30 | 937 |
| 23 | 2015-06-30 | 955 |
| 24 | 2015-03-31 | 940 |
| 25 | 2014-12-31 | 957 |
| 26 | 2014-09-30 | 852 |
| 27 | 2014-06-30 | 769 |
| 28 | 2014-03-31 | 621 |
| 29 | 2013-12-31 | 615 |
| 30 | 2013-09-30 | 431 |
| 31 | 2013-06-30 | 405 |
| 32 | 2013-03-31 | 562 |
| 33 | 2012-12-31 | 306 |
| 34 | 2012-09-30 | 50 |
| 35 | 2012-06-30 | 27 |
| 36 | 2012-03-31 | 30 |
| 37 | 2011-12-31 | 39 |
| 38 | 2011-09-30 | 58 |
| 39 | 2011-06-30 | 58 |
| 40 | 2011-03-31 | 49 |
| 41 | 2010-12-31 | 36 |
| 42 | 2010-09-30 | 31 |
| 43 | 2010-06-30 | 28 |
| 44 | 2010-03-31 | 21 |
| 45 | 2009-12-31 | |
| 46 | 2009-09-30 | 46 |
| 47 | 2009-06-30 | 27 |
| 48 | 2008-12-31 |
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
tesla_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 42 | 2010-09-30 | 31 |
| 43 | 2010-06-30 | 28 |
| 44 | 2010-03-31 | 21 |
| 46 | 2009-09-30 | 46 |
| 47 | 2009-06-30 | 27 |
Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is GameStop and its ticker symbol is GME.
game=yf.Ticker("GME")
Using the ticker object and the function history extract stock information and save it in a dataframe named gme_data. Set the period parameter to max so we get information for the maximum amount of time.
gme_data=game.history(period="max")
gme_data.reset_index(inplace=True)
gme_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2002-02-13 | 6.480513 | 6.773399 | 6.413183 | 6.766666 | 19054000 | 0.0 | 0.0 |
| 1 | 2002-02-14 | 6.850831 | 6.864296 | 6.682506 | 6.733003 | 2755400 | 0.0 | 0.0 |
| 2 | 2002-02-15 | 6.733001 | 6.749833 | 6.632006 | 6.699336 | 2097400 | 0.0 | 0.0 |
| 3 | 2002-02-19 | 6.665671 | 6.665671 | 6.312189 | 6.430017 | 1852600 | 0.0 | 0.0 |
| 4 | 2002-02-20 | 6.463681 | 6.648838 | 6.413183 | 6.648838 | 1723200 | 0.0 | 0.0 |
Use the requests library to download the webpage https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue. Save the text of the response as a variable named html_data.
html_data=requests.get("https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue").text
Parse the html data using beautiful_soup.
soup=BeautifulSoup(html_data)
Using beautiful soup extract the table with GameStop Quarterly Revenue and store it into a dataframe named gme_revenue. The dataframe should have columns Date and Revenue. Make sure the comma and dollar sign is removed from the Revenue column using a method similar to what you did in Question 2.
tables=soup.find_all('table')
gme_revenue=pd.DataFrame(columns=["Date","Revenue"])
for i,j in enumerate(tables):
if "GameStop Quarterly Revenue" in str(j):
ind=i
rows=tables[ind].find_all('tr')
for row in rows:
cols=row.find_all("td")
if (cols!=[]):
dat=cols[0].text
reve=cols[1].text.replace("$",'').replace(",","")
gme_revenue=gme_revenue.append({"Date":dat,"Revenue":reve},ignore_index=True)
gme_revenue.head()
| Date | Revenue | |
|---|---|---|
| 0 | 2021-04-30 | 1277 |
| 1 | 2021-01-31 | 2122 |
| 2 | 2020-10-31 | 1005 |
| 3 | 2020-07-31 | 942 |
| 4 | 2020-04-30 | 1021 |
Display the last five rows of the gme_revenue dataframe using the tail function. Take a screenshot of the results.
gme_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 61 | 2006-01-31 | 1667 |
| 62 | 2005-10-31 | 534 |
| 63 | 2005-07-31 | 416 |
| 64 | 2005-04-30 | 475 |
| 65 | 2005-01-31 | 709 |
Using the make_graph function to graph the Tesla Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(tesla_data, tesla_revenue, 'Tesla')
make_graph(tesla_data, tesla_revenue, 'Tesla')
Using the make_graph function to graph the GameStop Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(gme_data, gme_revenue, 'GameStop').
make_graph(gme_data, gme_revenue, 'GameStop')